package com.mastercard.labs

import grails.converters.JSON
import org.springframework.dao.DataIntegrityViolationException

class TransactionCategoryController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [transactionCategoryInstanceList: TransactionCategory.list(params), transactionCategoryInstanceTotal: TransactionCategory.count()]
    }

    def create() {
        [transactionCategoryInstance: new TransactionCategory(params)]
    }

    def save() {
        def transactionCategoryInstance = new TransactionCategory(params)
        if (!transactionCategoryInstance.save(flush: true)) {
            render(view: "create", model: [transactionCategoryInstance: transactionCategoryInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'transactionCategory.label', default: 'TransactionCategory'), transactionCategoryInstance.id])
        redirect(action: "show", id: transactionCategoryInstance.id)
    }

    def show() {
        def transactionCategoryInstance = TransactionCategory.get(params.id)
        if (!transactionCategoryInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'transactionCategory.label', default: 'TransactionCategory'), params.id])
            redirect(action: "list")
            return
        }

        [transactionCategoryInstance: transactionCategoryInstance]
    }

    def edit() {
        def transactionCategoryInstance = TransactionCategory.get(params.id)
        if (!transactionCategoryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'transactionCategory.label', default: 'TransactionCategory'), params.id])
            redirect(action: "list")
            return
        }

        [transactionCategoryInstance: transactionCategoryInstance]
    }

    def update() {
        def transactionCategoryInstance = TransactionCategory.get(params.id)
        if (!transactionCategoryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'transactionCategory.label', default: 'TransactionCategory'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (transactionCategoryInstance.version > version) {
                transactionCategoryInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'transactionCategory.label', default: 'TransactionCategory')] as Object[],
                          "Another user has updated this TransactionCategory while you were editing")
                render(view: "edit", model: [transactionCategoryInstance: transactionCategoryInstance])
                return
            }
        }

        transactionCategoryInstance.properties = params

        if (!transactionCategoryInstance.save(flush: true)) {
            render(view: "edit", model: [transactionCategoryInstance: transactionCategoryInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'transactionCategory.label', default: 'TransactionCategory'), transactionCategoryInstance.id])
        redirect(action: "show", id: transactionCategoryInstance.id)
    }

    def delete() {
        def transactionCategoryInstance = TransactionCategory.get(params.id)
        if (!transactionCategoryInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'transactionCategory.label', default: 'TransactionCategory'), params.id])
            redirect(action: "list")
            return
        }

        try {
            transactionCategoryInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'transactionCategory.label', default: 'TransactionCategory'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'transactionCategory.label', default: 'TransactionCategory'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
	
	def setTransactionCatgory() {
		def transaction = Transaction.get(params.transactionId)
		def category = Category.get(params.categoryId)
		def txnCategory = TransactionCategory.findByTransaction(transaction)
		if (txnCategory == null) {
			txnCategory = new TransactionCategory()
		}
		txnCategory.category = category
		txnCategory.transaction = transaction
		txnCategory.save()
		render (text: new JSON(txnCategory))
	}
}
